<script>
import { APP_NAME, HTTP_IMG_URL, appid } from 'config.js';
import { getSystemInfo } from '@/common/promise.js';

export default {
	globalData: {
		menuButtonInfo: {},
		safeArea: {},
		windowHeight: 0,
		windowWidth: 0,
		isIphoneX: false,
		image_path: HTTP_IMG_URL,
		app_name: APP_NAME,
		userInfo: uni.getStorageSync('userInfo'),
		token: uni.getStorageSync('token'),
		appid: appid,
		version: '',
		old_version: '',
		versionCode: 0,
		old_versionCode: 0,
	},
	onLaunch: async function() {
		console.log('App Launch');
		// const domModule = weex.requireModule('dom');
		// domModule.addRule('fontFace', {
		// 	fontFamily: 'iconfont',
		// 	src: 'http://at.alicdn.com/t/c/font_3340311_i4vsbgb23w.ttf'
		// 	// src: '../../static/style/ttf/iconfont.ttf'
		// });
	},
	onShow: async function() {
		console.log('onshow');
		// #ifdef MP-WEIXIN
		this.getMenuButtonInfo();
		// #endif
		this.getSystemInfo();
		this.checkPhoneType();
		// #ifdef APP-PLUS
		this.checkVersion();
		// #endif
	},
	onHide: function() {
		// console.log('App Hide')
	},

	methods: {
		// 获取胶囊按钮信息
		getMenuButtonInfo: function() {
			this.globalData.menuButtonInfo = uni.getMenuButtonBoundingClientRect();
		},

		// 获取终端系统信息
		getSystemInfo: async function() {
			let systemInfo = await getSystemInfo();
			return systemInfo;
		},

		// 检测手机型号
		checkPhoneType: async function() {
			let res = await getSystemInfo();
			if (res.model.search('iPhone X') != -1 || res.model.search('iPhone 11') != -1) {
				this.globalData.isIphoneX = true;
			} else {
				this.globalData.isIphoneX = false;
			}
		},

		// 检测更新
		// checkUpdate: function() {
		// 	let updateManager = uni.getUpdateManager();
		// 	updateManager.onCheckForUpdate(function(checkRes) {
		// 		// 请求完新版本信息的回调
		// 		if (checkRes.hasUpdate) {
		// 			updateManager.onUpdateReady(function(updateRes) {
		// 				uni.showModal({
		// 					title: '更新提示',
		// 					content: '版本更新，是否重启应用？',
		// 					success(res) {
		// 						if (res.confirm) {
		// 							// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
		// 							updateManager.applyUpdate();
		// 						}
		// 					}
		// 				});
		// 			});
		// 		}
		// 	});
		// },

		// 检测版本
		checkVersion: async function() {
			let _this = this;

			let res = await this.$api.user_api.getVersion();

			if (res.data.rcode == 1) {
				this.globalData.version = res.data.rdata.version;
				this.globalData.versionCode = res.data.rdata.versionCode;

				if (!uni.getStorageSync('now_version')) {
					// 判断是否已有版本更新记录

					// 默认缓存第一个版本
					uni.setStorage({
						key: 'now_version',
						data: this.globalData.version
					});
					uni.setStorage({
						key: 'now_versionCode',
						data: this.globalData.versionCode
					});
					return;
				}

				_this.globalData.old_version = uni.getStorageSync('now_version');
				_this.globalData.old_versionCode = uni.getStorageSync('now_versionCode');

				let reg2 = /[.]/gi; // 匹配全部 '.' =>''
				let new_version = this.globalData.version.replace(reg2, ''); //  1.0 =>10  版本格式转数字类型
				let old_version = this.globalData.old_version.replace(reg2, ''); //  1.0 =>10  版本格式转数字类型
				let new_version_arr = this.globalData.version.split('.'); // 1.0.0 => [1,0,0]  版本转换成数组
				let old_version_arr = this.globalData.old_version.split('.'); // 1.0.0 => [1,0,0]  版本转换成数组
				let log_data = res.data.rdata.remark.split(';'); // 更新日志

				if (
					parseInt(new_version) > parseInt(old_version) ||
					this.globalData.versionCode > _this.globalData.old_versionCode ||
					new_version_arr[0] > old_version_arr[0] ||
					new_version_arr[1] > old_version_arr[1]
				) {
					if (new_version_arr[0] > old_version_arr[0] || new_version_arr[1] > old_version_arr[1]) {
						// 大版本版本更新   弹窗更新提示
						let system = await getSystemInfo(); //  获取系统信息
						viewHotFixInit({
							systemInfo: system,
							log_data: log_data,
							version: _this.globalData.version,
							versionCode: _this.globalData.versionCode,
							apk_path: res.data.rdata.file_path
						});
						return;
					}

					uni.showLoading({
						title: '应用更新中...',
						mask: true
					});

					uni.downloadFile({
						url: res.data.rdata.newfile,
						success: async res_down => {
							uni.hideLoading();
							if (res_down.statusCode === 200) {
								uni.hideLoading();
								plus.nativeUI.showWaiting('正在更新文件...');

								plus.runtime.install(
									res_down.tempFilePath,
									{
										force: true
									},
									async function() {
										plus.nativeUI.closeWaiting();
										uni.setStorage({
											key: 'now_version',
											data: _this.globalData.version
										});
										uni.setStorage({
											key: 'now_versionCode',
											data: _this.globalData.versionCode
										});

										setTimeout(() => {
											plus.runtime.restart();
										}, 1000);
									},
									function(e) {
										uni.showToast({
											icon: 'error',
											image: '../static/images/error.png',
											title: `热更新失败:${e.message}`,
											mask: true,
											duration: 2000
										});
									}
								);
							}
						}
					});
				} else {
				}
			}
		}
	}
};
</script>

<style>
/*每个页面公共css */

/* #ifndef APP-NVUE */
@import url('/static/style/css/base.css');
@import url('/static/style/css/module.css');
@import url('/static/style/css/form.css');
@import url('/static/style/css/popup.css');
@import url('/static/style/css/iconfont.css');
@import url('/static/style/css/animate.min.css');
/* #endif */

/* #ifdef MP-WEIXIN */
uni-page-body,
#app {
	height: 100%;
}
/* #endif */
</style>
